/**
*@author : MAMAMA
*@date : 2019-07-17
*@description : spi driver
*/

#include "sspi.h"

static SpiPort_T Spi3Ports= {
    .MOSI = {GPIOB, 5, GPIO_Pin_5},
    .MISO = {GPIOB, 4, GPIO_Pin_4},
    .SCK = {GPIOB, 3, GPIO_Pin_3},
    .NSS = {GPIOB, 6, GPIO_Pin_6}
};

void Board_SPI1_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;
    
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PA5 - SPI_CLK
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PA6 - SPI_MISO
    GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PA7 - SPI_MOSI
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Low_Speed;//2MHz
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //PA4 - SPI_CS
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;//CRC calculate value, not used. 
                                                                                    //start this function with SPI_TransmitCRC and SPI_CalculateCRC
    SPI_Init(SPI1, &SPI_InitStructure);
    
    SPI_Cmd(SPI1, ENABLE); 
}

void Board_SPI2_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;
    
    //使能外设时钟
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽方式
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);  //PC2|PC3 - AF
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_Init(GPIOB, &GPIO_InitStructure);  //PB10 - AF
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Init(GPIOC, &GPIO_InitStructure); //PC1 - Output    
        
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_SPI2); //PB10 - CLK
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource2,GPIO_AF_SPI2);  //PC2 - MISO
    GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_SPI2);  //PC3 - MOSI
    
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;  //clock normal high
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //clock phase
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;//CRC calculate value, not used. 
                                                                                    //start this function with SPI_TransmitCRC and SPI_CalculateCRC
    SPI_Init(SPI2, &SPI_InitStructure);
    
    SPI_Cmd(SPI2, ENABLE); 
}

void Board_SPI3_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
    
    /* 设置为复用功能 */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI3);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI3);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI3);
    
    GPIO_InitStructure.GPIO_Pin = Spi3Ports.MOSI.pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(Spi3Ports.MOSI.port, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = Spi3Ports.MISO.pin;
    GPIO_Init(Spi3Ports.MISO.port, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = Spi3Ports.SCK.pin;
    GPIO_Init(Spi3Ports.SCK.port, &GPIO_InitStructure);
    
    /* PA15 - IMU chip select */
    GPIO_InitStructure.GPIO_Pin = Spi3Ports.NSS.pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_Init(Spi3Ports.NSS.port, &GPIO_InitStructure);

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    /*
    SPI3由APB1外设时钟控制，APB1预分频默认为4，因此APB1的PCLK1 = 168/4 = 42MHz
    SPI3的时钟由PCLK1分频，如果SPI分频系数为16，则SPI时钟为42/16 = 2.625MHz
    */
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;//CRC calculate value, not used. 
                                                                                    //start this function with SPI_TransmitCRC and SPI_CalculateCRC
    SPI_Init(SPI3, &SPI_InitStructure);
    
    SPI_Cmd(SPI3, ENABLE);
}

void Board_SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
    SPI1->CR1&=0XFFC7;
    SPI1->CR1|=SPI_BaudRatePrescaler;    
    SPI_Cmd(SPI1,ENABLE); 
} 

#define SPI_TIMEOUT_MAX 20000

/**
 @description write one byte to spi tx pin and read one byte simultaneously
 @return recieved byte
*/
u8 Board_SPI1_ReadWriteByte(u8 TxData)
{ 
    u32 timeout = SPI_TIMEOUT_MAX;
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){if((timeout--)==0)return 0xFF;};    //wait until sending finish    
    SPI_I2S_SendData(SPI1, TxData);
    timeout = SPI_TIMEOUT_MAX;
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){if((timeout--)==0)return 0xFF;}; //wait until data received 
    return SPI_I2S_ReceiveData(SPI1);
}

u8 Board_SPI3_ReadWriteByte(u8 TxData)
{ 
    u32 timeout = SPI_TIMEOUT_MAX;
    while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET){if((timeout--)==0)return 0xFF;};    //wait until sending finish    
    SPI_I2S_SendData(SPI3, TxData);
    timeout = SPI_TIMEOUT_MAX;
    while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET){if((timeout--)==0)return 0xFF;};  //wait until data received 
    return SPI_I2S_ReceiveData(SPI3);
}

/**
 @description write two bytes to spi tx pin and read two bytes simultaneously
 @return recieved u16 data
*/
u8 Board_SPI2_ReadWriteWord(u16 TxData)
{ 
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);    //wait until sending finish    
    SPI_I2S_SendData(SPI2, TxData);         
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //wait until data received 
    return SPI_I2S_ReceiveData(SPI2);
}
